//
//  Bundle+Extension.swift
//  UIKitExtentions
//
//  Created by Alean on 2022/12/22.
//

import Foundation

public extension WDExtension where Base: Bundle {
    
}

let versionKey = "versionKey"
public extension Bundle {
    //MARK:--- Pod Bundle 检索 ----------
    /// - from:bundle.url(forResource ‘(pod s.resource_bundles -> key)’
    static func cd_bundle(_ forClass:AnyClass, _ from:String? = nil) -> Bundle? {
        let bundle = Bundle(for: forClass)
        guard let bundleURL = bundle.url(forResource: from, withExtension: "bundle") else {
            //assertionFailure("👉👉👉\(from) - 无法找到 Bundle  👻")
            return nil
        }
        return Bundle(url: bundleURL)
    }
    
    // MARK: - 取到版本,并与上次比较
    var isNewFeature: Bool {
        let newVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
        let oldVersion = UserDefaults.standard.value(forKey: versionKey) as? String

        if oldVersion == nil || oldVersion! != newVersion {
            //将新版本存起来
            UserDefaults.standard.setValue(newVersion, forKey: versionKey)
            return true
        }
        return false
    }
}
